/**
 * create by young (young2f@outlook.com) on 2023/5/4
 *         deviceSpecCode: {type: String, desc: '设备规格编码'},
 deviceCode: {type: String, required: true, desc: '设备编码'},
 deviceType: {type: String, desc: '设备类型'},
 tenantCode: {type: String, required: true, desc: '租户编码'},
 tenantName: {type: String},
 deviceStatusIndex:{type:Number},
 eventName: {type: String, default: '待机中'},//事件名称
 eventType: {type: String, default: 'OEE'},//事件类型
 startTs: {type: Number, default: 0},
 endTs: {type: Number, default: 0},
 name: {type: String},
 path: {type: String},
 value: {type: Number},
 ruleValue: {type: String},
 rule: {type: String},
 ruleCode: {type: String},
 */
'use strict';
const moment = require('moment');


var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var req = new XMLHttpRequest();
const tenant = {'tenantCode': 'TEN23042822280074F9',};
req.onreadystatechange = function () {
    console.log(req.status);
    if (req.readyState == 4 && req.status == 200) {
        const devices = JSON.parse(req.responseText);
        console.log('get devices', req.responseText);
        console.log(devices.length);
        if (devices) {
            devices.result.forEach(item => {
                createDeviceEvent(item);
            });
        }
    }
};

req.open('POST', 'http://127.0.0.1:18008/api/device/find/all', true);
req.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
req.send(JSON.stringify(tenant));


function rand(min, max)  {
    return Math.ceil(Math.random() * (max-min+1) + min-1)
}
console.log(rand(50,70))
return
function createDeviceEvent(device) {
    const events = getOEEGanttTestData(device);
    events.forEach(item => {
        createEvent(item);
    });
}

function createEvent(item) {
    var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
    var req = new XMLHttpRequest();
    req.onreadystatechange = function () {
        console.log(req.status);
        if (req.readyState == 4 && req.status == 200) {
            console.log(req.responseText);
        }
    };
    req.open('POST', 'http://127.0.0.1:18008/api/device/event/create', true);
    req.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
    req.send(JSON.stringify(item));

}

console.log(getOEEGanttTestData({deviceName: 'ddd', deviceCode: '123123'}));

function getOEEGanttTestData(device) {
    const Colors = [
        '#52c41a',
        '#fadb14',
        '#fa541c'
    ];
    const {deviceName, deviceCode, tenantCode, tenantName, deviceType} = device;
    const data = [];
    const str = moment().add(-1, 'day').utc().startOf('days').toISOString();
    const activeTs = 1000000;
    const stopTs = 100000;
    let time = moment(str).valueOf();
    const num=rand(50,70)
    for (let i = 0; i < num; i++) {
        let deviceStatusIndex = i % 2 === 0 ? 0 : 1;
        deviceStatusIndex = deviceName.length > 7 && i == deviceName.length ? 2 : deviceStatusIndex;
        const rand = i % 2 === 0 ? 800000 : 100000;
        const r = Math.floor(Math.random() * rand);
        const startTs = time + Math.floor(Math.random() * 10000);
        let length = deviceStatusIndex === 0 || deviceStatusIndex === 2 ? activeTs : stopTs;
        let endTs = moment(startTs).valueOf() + length + r;
        if (moment(startTs).hour() === 12) {
            deviceStatusIndex = 1;
            length = 1000 * 60 * 45;
            endTs = moment(startTs).valueOf() + length + r;
        }
        time = endTs;
        let eventName = deviceStatusIndex === 0 ? '生产中' : '待机中';
        eventName = deviceStatusIndex === 2 ? '故障中' : eventName;
        const temp = {
            deviceStatusIndex,
            deviceName: device.deviceName,
            eventName,
            startTs,
            endTs,
            statusColor: Colors[deviceStatusIndex],
            eventType: 'OEE',
            deviceCode, tenantCode, tenantName, deviceType
        };
        data.push(temp);
    }
    return data;
}
